#!/bin/bash

# build native icu first
pushd icu/source
mkdir -p native-build
pushd native-build
../configure --prefix $PWD/install
make -j
popd
popd

. setup.sh

# patch Makefile.inc of icu lib
mkdir -p ${TARGET_ROOT}
cat << EOF > ${TARGET_ROOT}/postinst
#!/bin/bash
set -Eeuo pipefail
_prefix=\${1:-}
if [ -z "\$_prefix" ]; then
	echo "ERROR: empty prefix in 1st parameter"
	exit 1
fi
final_mk=\$(readlink -f \${_prefix}/${OHOS_LIBDIR}/icu/Makefile.inc)
sed -i -e "s|^\(libdir[[:space:]]*=[[:space:]]*\).*|\1\\\${prefix}/${OHOS_LIBDIR}|g" \
	-e "s|^\(prefix[[:space:]]*=[[:space:]]*\).*|\1\${_prefix}|g" \
	\${final_mk}
EOF
if [ -f icu/source/Makefile ]; then
	pushd icu/source
	make clean
	popd
fi
build_makeproj_with_deps "icu" "" "--with-cross-build=${CUR_DIR}/icu/source/native-build" "" "" "" "icu/source"

build_cmakeproj_with_deps "libxml2" "libiconv icu readline ncurses" "-DLIBXML2_WITH_HISTORY=ON -DLIBXML2_WITH_ICU=ON"

build_makeproj_with_deps "asio" "" "" "./autogen.sh" "" "" "asio/asio"

build_cmakeproj_with_deps "console_bridge" "" "-DBUILD_SHARED_LIBS=ON"

# make nasm configure happy
if [ ! -f nasm/nasm.1 ]; then
	touch nasm/nasm.1 nasm/ndisasm.1
fi
build_makeproj_with_deps "nasm" "" "" "./autogen.sh"

INSTALL_USER=root INSTALL_GROUP=root build_makeproj_with_deps "attr"
build_makeproj_with_deps "acl" "attr"
build_cmakeproj_with_deps "assimp" "" "-DBUILD_SHARED_LIBS=ON"
build_cmakeproj_with_deps "fmt" "" "-DBUILD_SHARED_LIBS=ON"
build_cmakeproj_with_deps "yaml-cpp" "" "-DBUILD_SHARED_LIBS=ON"

build_makeproj_with_deps "libpsl" 
# patch curl
if [ ! -f curl/patched ]; then
	patch -p0 --dry-run < patches/oh-curl.patch
	patch -p0 < patches/oh-curl.patch
	touch curl/patched
fi
build_cmakeproj_with_deps "curl" "openssl libpsl" "-DBUILD_SHARED_LIBS=ON"

# patch boost
if [ ! -f boost/patched ]; then
	patch -p0 --dry-run < patches/oh-boost-1.81.0.patch
	patch -p0 < patches/oh-boost-1.81.0.patch
	touch boost/patched
fi
pushd boost
BOOST_PREFIX=${TARGET_ROOT}.boost

./bootstrap.sh --with-toolset=gcc --prefix=$BOOST_PREFIX --libdir=$BOOST_PREFIX/${OHOS_LIBDIR} --without-libraries=log,locale
sed -i '/.*using gcc ;/c\    using gcc : '"${OHOS_CPU}"' : '"${OHOS_SDK}"'/native/llvm/bin/'"${OHOS_CPU}"'-unknown-linux-ohos-clang ; ' project-config.jam
./b2 cxxflags="$CXXFLAGS" cflags="$CFLAGS" variant=release
./b2 install

popd

build_cmakeproj_with_deps "eigen" "gmp mpfr boost" "-DBUILD_SHARED_LIBS=ON"

# patch qhull
sed -i 's|set\(PkgConfigLocation lib/pkgconfig\)|set(PkgConfigLocation ${LIB_INSTALL_DIR}/pkgconfig)|g' qhull/CMakeLists.txt
build_cmakeproj_with_deps "qhull" "" "-DBUILD_SHARED_LIBS=ON -DLIB_INSTALL_DIR=${OHOS_LIBDIR}"
# remove the fucking idiot cmake config generated by qhull
rm -rf ${TARGET_ROOT}.qhull/lib/{cmake,pkgconfig}

# patch libccd for libm
sed -i 's|^if\(NOT WIN32\)|if(NOT WIN32 AND NOT OHOS)|g' libccd/src/CMakeLists.txt
build_cmakeproj_with_deps "libccd" "" "-DBUILD_SHARED_LIBS=ON"

build_cmakeproj_with_deps "SuiteSparse" "gmp mpfr OpenBLAS" "-DBUILD_SHARED_LIBS=ON -DGRAPHBLAS_USE_JIT=OFF"


build_cmakeproj_with_deps "gflags" "" "-DBUILD_SHARED_LIBS=ON -DGFLAGS_LIBRARY_INSTALL_DIR=${OHOS_LIBDIR}"
build_cmakeproj_with_deps "glog" "gflags" "-DBUILD_SHARED_LIBS=ON"

build_cmakeproj_with_deps "ceres-solver" "gflags glog eigen OpenBLAS SuiteSparse" "-DBUILD_SHARED_LIBS=ON"

# patch zstd
sed -i '244c#if defined(__OPENHARMONY__) || (!defined(_GNU_SOURCE) && !defined(__APPLE__) && !defined(_MSC_VER))' zstd/lib/dictBuilder/cover.c
sed -i '331c#elif defined(_GNU_SOURCE) && !defined(__OPENHARMONY__)' zstd/lib/dictBuilder/cover.c
build_cmakeproj_with_deps "zstd" "" "-DBUILD_SHARED_LIBS=ON -S build/cmake -G Ninja"

build_cmakeproj_with_deps "zeromq" "" "-DBUILD_SHARED_LIBS=ON"

build_cmakeproj_with_deps "libexpat" "" "-DBUILD_SHARED_LIBS=ON -S expat"

build_cmakeproj_with_deps "libpng" "" "-DBUILD_SHARED_LIBS=ON"

build_cmakeproj_with_deps "g2o" "eigen SuiteSparse" "-DBUILD_SHARED_LIBS=ON"

build_cmakeproj_with_deps "geographiclib" "" "-DBUILD_SHARED_LIBS=ON -DCMAKEDIR=${OHOS_LIBDIR}/cmake/GeographicLib -DPKGDIR=${OHOS_LIBDIR}/pkgconfig"
build_cmakeproj_with_deps "tinyxml2" "" "-DBUILD_SHARED_LIBS=ON"

# patch oneTBB: OH doesn't support pvmalloc or resumable tasks
sed -i '/__OPENHARMONY__/! s/^\(#define __TBB_RESUMABLE_TASKS\s*(\)\(!__TBB_WIN8UI_SUPPORT\)/\1 !__OPENHARMONY__ \&\& \2/' oneTBB/include/oneapi/tbb/detail/_config.h
sed -i '/__OPENHARMONY__/! s/^\(#if (!__TBB_WIN8UI_SUPPORT \&\& !defined(WINAPI_FAMILY) \&\& !__ANDROID__\))/\1 \&\& !__OPENHARMONY__)/' oneTBB/test/conformance/conformance_resumable_tasks.cpp
sed -i '/__OPENHARMONY__/! s/^\(#define __TBB_PVALLOC_PRESENT\s\+__unix__ \&\& !__ANDROID__\)/\1 \&\& !__OPENHARMONY__/' oneTBB/test/tbbmalloc/test_malloc_overload.cpp
build_cmakeproj_with_deps "oneTBB" "" "-DBUILD_SHARED_LIBS=ON" "" "" "" "" "16"

build_cmakeproj_with_deps "pcre2" "bzip2 readline ncurses" "-DBUILD_SHARED_LIBS=ON -DPCRE2_STATIC_PIC=ON"

build_cmakeproj_with_deps "swig" "pcre2" "-DBUILD_SHARED_LIBS=ON"

# patch YDLidar-SDK for not supporting libdir
sed -i -e "\|${OHOS_LIBDIR}|! s|\(\${CMAKE_INSTALL_PREFIX}\)/lib[[:space:]]*|\1/${OHOS_LIBDIR}|" \
	-e "\|${OHOS_LIBDIR}|! s|lib\(/cmake/\${PROJECT_NAME}\)|${OHOS_LIBDIR}\1|" \
	YDLidar-SDK/cmake/install_package.cmake
# OH doesn't support pthread_cancel and pthread_testcancel
sed -i -e '/__OPENHARMONY__/! s/^\(#if defined(__ANDROID__)\)$/\1 || defined(__OPENHARMONY__)/' \
	-e '159 { /__OPENHARMONY__/! s/\(#if defined(_WIN32)\)/\1 || defined(__OPENHARMONY__)/ }' \
	YDLidar-SDK/core/base/thread.h
# idiot YDLidar-SDK example cmake file will cause C compiler to crash
sed -i "s/^add_compile_options(-std=c++11)/set(CMAKE_CXX_STANDARD 11)\nset(CMAKE_CXX_STANDARD_REQUIRED ON)/" \
	YDLidar-SDK/examples/CMakeLists.txt
#build_cmakeproj_with_deps "YDLidar-SDK" "Python" "-DBUILD_SHARED_LIBS=ON -DPYTHON_EXECUTABLE=${TARGET_ROOT}.Python/bin/python3 -DPYTHON_LIBRARY=${TARGET_ROOT}.Python/${OHOS_LIBDIR}/libpython3.so"
build_cmakeproj_with_deps "YDLidar-SDK" "" "-DBUILD_SHARED_LIBS=ON" "" "-Wno-format-security"

# OH doesn't support pthread_setaffinity_np
sed -i '/__OPENHARMONY__/! s/\(#if defined(__x86_64__) \&\& defined(__linux__) \&\& !defined(__ANDROID__)\)$/\1 \&\& !defined(__OPENHARMONY__)/g' llama.cpp/common/common.cpp
build_cmakeproj_with_deps "llama.cpp" "openssl curl OpenBLAS" "-DBUILD_SHARED_LIBS=ON -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS"

build_makeproj_with_deps "rsync" "openssl zstd" "--disable-md2man --disable-xxhash --disable-lz4" "" "" "1"

build_cmakeproj_with_deps "lz4" "" "-S build/cmake -DBUILD_SHARED_LIBS=ON"
